package net.sf.brutil.v0.light;

import java.io.PrintStream;

public class Logger {

	static PrintStream out=System.out;
	
	
	int minimumlevel=0;
	
	final static int DEBUG=0;
	final static int TRACE=1;
	final static int INFO=2;
	final static int WARN=3;
	final static int ERROR=4;
    
	
	public  boolean isLevelEnabled(int level){
        return level>=minimumlevel;
    }
    
	
	public  boolean isTraceEnabled(){
		return isLevelEnabled(TRACE);
	}
	public  boolean isDebugEnabled(){
	    return isLevelEnabled(DEBUG);
	}
	public  boolean isInfoEnabled(){
	    return isLevelEnabled(INFO);
	}
	
	Class<?>clazz;
	public static Logger getLogger(Class<?>clazz){
		Logger retVal= new Logger();
		retVal.clazz=clazz;
		return retVal;
	}
	private Logger(){
		
	}
	public void log(int level,Object ...data){
	    if (isLevelEnabled(level)){
	    
	        String levelString=getLevelAsString(level);
	    
    		if (out!=null) {
    			out.print("\n["+levelString+"] "+clazz.getSimpleName()+" ");
    			for(Object string:data){
    				out.print(""+string);
    				out.print(" ");
    			}	
    		}
	    }
	}
	
	public void logException(int level,Throwable e){
	    if (isLevelEnabled(level)){
	        if (e!=null && out!=null)
	            e.printStackTrace(out);
	    }
        
	}
	    
	
	    
	public String getLevelAsString(int level) {
        
	    switch(level){
	    case DEBUG: return "DEBUG";
	    case TRACE: return "TRACE";
	    case INFO: return  "INFO ";
	    case WARN: return  "WARN ";
	    case ERROR: return "ERROR";        
	    default:  return ""+level;
	    }
    }


    public  void trace(Object ...data){
			log(TRACE,data);
	}
	public  void debug(Object ...data){
				log(DEBUG,data);
	}
	public  void info(Object ...data){
			log(INFO,data);
	}
	public  void warn(Object ...data){
			log(WARN,data);
	}
	
	
	public  void error(String message, Throwable e) {
		log(ERROR,message);
		logException(ERROR,e);
	}
}
